package com.halopay.service.lmpl;


import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.plugins.Page;
import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import com.halopay.common.utils.DateUtils;
import com.halopay.common.utils.PageUtils;
import com.halopay.common.utils.Query;
import com.halopay.entity.Packet;
import com.halopay.entity.ScheduleJobEntity;
import com.halopay.mapper.PacketMapper;
import com.halopay.service.PacketService;
import com.halopay.service.ScheduleJobService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.Map;



/**
 * @author mingpingqiang
 */
@Service("packetService")
public class PacketServiceImpl extends ServiceImpl<PacketMapper, Packet> implements PacketService {
    @Autowired
    private ScheduleJobService scheduleJobService;

    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        Page<Packet> page = this.selectPage(
                new Query<Packet>(params).getPage(),
                new EntityWrapper<Packet>()
        );

        return new PageUtils(page);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void createPacket(ScheduleJobEntity scheduleJobEntity, Packet packet) {
        this.insert(packet);
        Date startTime = packet.getStartTime();
        String dateStart = DateUtils.dateToString(startTime,DateUtils.cronFormat);
        scheduleJobEntity.setBeanName("packetTask");
        scheduleJobEntity.setMethodName("packet");
        scheduleJobEntity.setCronExpression(dateStart);
        scheduleJobEntity.setParams(String.valueOf(packet.getId()));
        scheduleJobEntity.setRemark(DateUtils.dateToString(startTime)+"红包雨开始！");
        scheduleJobService.insert(scheduleJobEntity);
        scheduleJobService.save(scheduleJobEntity);
        packet.setJobId(scheduleJobEntity.getJobId());
        this.updateById(packet);
    }

}
